package com.jfinal.bbsapp.common.kit;
import java.text.DecimalFormat;

import org.apache.log4j.Logger;

public class StringUtils {
	private static final Logger logger = Logger.getLogger(StringUtils.class);
	
	//从字符串中取前几个字后面用“...代替”
	public static String getleft(String str) {
		String restr = "";
		try {
			if (str == null || str.isEmpty()) {
				restr = "";
	        }else {
	        	restr = str.length() > 10 ? str.substring(0, 10)+"..." : str + "-";
	        }
		} catch (Exception e) {
			logger.error("------Exception------", e);
		}
		return restr;
	}
	
	//从字符串中取前几个字后面用“...代替”
	public static String getleft2(String str) {
		String restr = "";
		try {
			if (str == null || str.isEmpty()) {
				restr = "";
	        }else {
	        	restr = str.length() > 10 ? str.substring(0, 10)+"..." : "-" +str;
	        }
		} catch (Exception e) {
			logger.error("------Exception------", e);
		}
		return restr;
	}
	
	//有小数点显示没有则不显示小数点
	public static String formatNumber(double number) {
        // 创建DecimalFormat实例
        DecimalFormat dfWithDecimals = new DecimalFormat("0.00");
        DecimalFormat dfWithoutDecimals = new DecimalFormat("0");

        // 检查小数部分
        if (number % 1 == 0) {
            return dfWithoutDecimals.format(number);  // 无小数部分
        } else {
            return dfWithDecimals.format(number);      // 有小数部分
        }
    }
	
	//手机号码中间4位用****代替
	public static String mobileformat(String mobile) {
		String restr = "";
		try {
			if (mobile == null || mobile.isEmpty()) {
				restr = "";
	        }else {
	        	 //截取前三位
	            String beginStr = mobile .substring(0,3);
	            //截取后四位
	            String endStr = mobile .substring(7);
	            restr = beginStr + "****" + endStr;
	        }
		} catch (Exception e) {
			logger.error("------Exception------", e);
		}
		return restr;
	}
	
	
}
